package com.orifine.sql.chain;

import java.util.ArrayList;
import java.util.List;

import com.orifine.orm.model.TableModel;
import com.orifine.sql.SyntaxString;

public class ChainFrom implements ChainSql {
	private List<String> fromTableNames = new ArrayList<String>();
	public ChainFrom from(TableModel table){
		fromTableNames.add(table.getName());
		return this;
	}
	
	private static List<ChainJoin> joins = new ArrayList<ChainJoin>();
	public ChainFrom innerJoinOn(TableModel table, ChainOn on){
		joins.add(new ChainJoin(ChainJoinType.INNER_JOIN, table, on));
		return this;
	}
	
	public ChainFrom leftJoinOn(TableModel table, ChainOn on){
		joins.add(new ChainJoin(ChainJoinType.LEFT_JOIN, table, on));
		return this;
	}
	
	public ChainFrom rightJoinOn(TableModel table, ChainOn on){
		joins.add(new ChainJoin(ChainJoinType.RIGHT_JOIN, table, on));
		return this;
	}
	
	public ChainFrom fullJoinOn(TableModel table, ChainOn on){
		joins.add(new ChainJoin(ChainJoinType.FULL_JOIN, table, on));
		return this;
	}

	@Override
	public void chain(SyntaxString out) {
		boolean isFirst = true;
		out.addWord("FROM");
		isFirst = true;
		for(String fromTableName : fromTableNames){
			if(isFirst){
				isFirst = false;
			} else {
				out.append(",");
			}
			out.addWord(fromTableName);
		}
	}

	@Override
	public String sql() {
		SyntaxString syntaxString = new SyntaxString();
		chain(syntaxString);
		return syntaxString.toString();
	}
}
